/**
 * 
 */
package br.embedded.socialapp.business;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.TimeZone;

import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultEdge;

import br.embedded.socialapp.data.DataFacade;
import br.embedded.socialapp.domain.Usuario;
import br.embedded.socialapp.exceptions.UserException;
import br.embedded.socialapp.location.Coordenates;
import br.embedded.socialapp.location.LocationManager;

/**
 * @author daniel
 *
 */
public class Generator
{
	public static void main(String[] args)
	{
		List<Usuario> usersList = UserGenerator.generateUsers(100);
		List<Usuario> list = new ArrayList<Usuario>(usersList);
		
		addUsers(usersList);
		Graph<Usuario, DefaultEdge> graph = UserGenerator.buildFriendshipGraph(usersList, 10);
		
		for (DefaultEdge edge :  graph.edgeSet()) {
			Usuario u1 = graph.getEdgeSource(edge);
			Usuario u2 = graph.getEdgeTarget(edge);
			
			DataFacade.getInstance().addFriendship(u1.getLogin(), u2.getLogin(), "myspace");
		}
		
		//System.out.println(FriendManager.getInstance().getFriends(list.get(0)));
		//System.out.println(FriendManager.getInstance().getFriends(list.get(3)));
				
		//List<List<Coordenates>> listCoordenates = CoordinatesGenerator.generateCoordinates(-7.212466615909, -35.908287420042,
		//					-7.227505599999965, -35.89469999999955,
		//					2501.0, -0.00005, 1000);
		
		
		//System.out.println("In range coordinates: " + listCoordenates.get(0).size());
		//System.out.println("Not in range coordinates: " + listCoordenates.get(1).size());
		
/*		Calendar calendar = new GregorianCalendar();
		
		long data1 = calendar.getTimeInMillis();
		calendar.add(Calendar.SECOND, 30);
		long data2 = calendar.getTimeInMillis();
		
		while (true)
		{	
			try {
				Thread.sleep(30000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			System.out.println(dateToString(new Date(data1)));
			System.out.println(dateToString(new Date(data2)));
		
			Coordenates c = new Coordenates();
			c.setLatitude(-7.212466615909);
			c.setLongitude(-35.908287420042);
			c.setUserLogin(list.get(0).getLogin());
			
			List<Coordenates> l = LocationManager.getInstance().getNearestUsers(c, new Date(data1), new Date(data2), 1500);
			
			for (Coordenates coordenates : l) {
				Date d = new Date(coordenates.getDate());
				System.out.println(coordenates.getUserLogin() + " - " + dateToString(d) + " - " + coordenates.getDistance());
			}
			
			data1 = data2;
			calendar.add(Calendar.SECOND, 30);
			data2 = calendar.getTimeInMillis();
		}
/*
/*		
		for (int i = 0; i < 10; i++) {
			int index = CoordinatesGenerator.getCoordinate(listCoordenates.get(0));
			Coordenates c = listCoordenates.get(0).get(index);
			//System.out.println(c.getDistance());
		}
		
		for (int i = 0; i < 10; i++) {
			int index = CoordinatesGenerator.getCoordinate(listCoordenates.get(1));
			Coordenates c = listCoordenates.get(1).get(index);
			//System.out.println(c.getDistance());
			
			//int j = Collections.binarySearch(listCoordenates.get(1), c);
			//System.out.println(index == j);
		}
/*		
		int index = UserGenerator.getUser(listUsers);
		Usuario u = listUsers.get(index);
		
		Graph<Usuario, DefaultEdge> friendGraph = UserGenerator.buildFriendshipGraph(listUsers, 100);
		System.out.println(friendGraph);
*/		
	}
	
	private static String dateToString(Date date)
	{
		SimpleDateFormat myFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		myFormat.setTimeZone(TimeZone.getTimeZone ("GMT-3"));
		return myFormat.format(date);
	}
	
	public static Coordenates nextCoordenate(List<Coordenates> list, int current)
	{
		if (current < list.size() - 1)
			return list.get(++current);
		
		return null;
	}
	
	private static void addUsers(List<Usuario> usersList)
	{
		for (Usuario usuario : usersList) {
//			try {
//				UserManager.getInstance().createUser(usuario.getLogin(), usuario.getFirstName(), 
//													 usuario.getLastName(), usuario.getPasswd());
//				
				DataFacade.getInstance().updateSocialNetworkId(usuario.getLogin(), 
															   "myspace", usuario.getLogin());
//			} catch (UserException e) {
//				e.printStackTrace();
//			}
		}
	}
}
